<template>
  <div class="un">
    <!-- 工具栏和搜索栏 -->
    <el-form :inline="true" :model="keyword" ref="searchForm" class="demo-form-inline">
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="openAddDialog"
        >添加
        </el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          @click="delSelect"
          :disabled="this.multipleSelection.length==0?true:false"
        >批量删除
        </el-button
        >
      </el-form-item>
      <el-form-item label="单位编码" prop="measureCode">
        <el-input size="small" clearable v-model="keyword.measureCode" placeholder="单位编码"></el-input>
      </el-form-item>
      <el-form-item label="单位名称" prop="measureName">
        <el-input size="small" clearable v-model="keyword.measureName" placeholder="单位名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="search" icon="el-icon-search">查询</el-button>
        <el-button type="primary" plain size="small" @click="resetSearchForm('searchForm')" icon="el-icon-refresh">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <!--添加和修改 的对话框-->
    <el-dialog :title="dialogName" :visible.sync="addDialog" width="35%">
      <el-form :model="oneMeasure" :rules="rules" ref="oneMeasureForm">
        <el-form-item label="单位编码" prop="measureCode" label-width="160px">
          <el-input
            v-model="oneMeasure.measureCode"
            autocomplete="off"
            style="width: 200px"
            placeholder="请输入编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="单位名称" prop="measureName" label-width="160px">
          <el-input
            v-model="oneMeasure.measureName"
            autocomplete="off"
            style="width: 200px"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否主单位" prop="primaryFlag" label-width="160px">
          <el-input
            v-model="oneMeasure.primaryFlag"
            autocomplete="off"
            style="width: 200px"
            placeholder="请输入Y或N"
          ></el-input>
        </el-form-item>
        <el-form-item label="与主单位换算比例" label-width="160px">
          <el-input
            v-model="oneMeasure.changeRate"
            autocomplete="off"
            style="width: 200px"
            placeholder="请输入比例"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="enableFlag" label-width="160px">
          <el-input
            v-model="oneMeasure.enableFlag"
            autocomplete="off"
            style="width: 200px"
            placeholder="请输入Y或N"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="160px">
          <el-input
            v-model="oneMeasure.remark"
            type="textarea"
            maxlength="30"
            show-word-limit
            autocomplete="off"
            style="width: 200px;"
            placeholder="请输入备注信息"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddMeasure('oneMeasureForm')">取 消</el-button>
        <el-button type="primary" @click="addMeasure('oneMeasureForm')"
        >提交
        </el-button
        >
      </div>
    </el-dialog>
    <!-- 数据展示栏 -->
    <el-table v-loading="loading" :data="measureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="单位编码" align="center" prop="measureCode"/>
      <el-table-column label="单位名称" align="center" prop="measureName"/>
      <el-table-column label="是否是主单位" align="center" prop="primaryFlag">
        <template slot-scope="scope">
          <el-tag :type="scope.row.primaryFlag == 'Y'?'':'danger'">
            <span>{{ scope.row.primaryFlag == 'Y' ? '是' : '否' }}</span></el-tag>
        </template>
      </el-table-column>
      <el-table-column label="与主单位换算比例" align="center" prop="changeRate"/>
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <el-tag :type="scope.row.enableFlag == 'Y'?'':'danger'">
            <span>{{ scope.row.enableFlag == 'Y' ? '是' : '否' }}</span></el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning" plain
            icon="el-icon-edit"
            @click="handleUpdate('编辑单位',scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="danger" plain
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getUmList, delUmone, delMany, addUmone, updateUmone } from '@/api/umdata'

export default {
  name: 'Unit',
  data() {
    return {
      // 遮罩层
      loading: true,
      pageNum: 1,
      pageSize: 5,
      currentPage: 1, // 当前页面
      total: 0, // 总条数
      pageNum: 1, // 当前页面
      pageSize: 5, // 每页大小
      addDialog: false,
      dialogName: '添加单位',
      measureList: [],
      measureCodes: [],
      multipleSelection: [],
      keyword: {
        measureCode: '',
        measureName: ''
      },
      oneMeasure: {
        measureCode: '',
        measureName: '',
        primaryFlag: '',
        changeRate: '',
        enableFlag: '',
        remark: ''
      },
      rules: {
        measureCode: [
          {
            required: true,
            message: '请输入单位编码',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 5,
            message: '长度在 1 到5 个字符',
            trigger: 'blur'
          }
        ],
        measureName: [
          { required: true, message: '请输入单位名称', trigger: 'blur' }
        ],
        primaryFlag: [
          {
            required: true,
            min: 1,
            max: 1,
            message: '只能输入Y或N！',
            trigger: 'blur'
          }
        ],
        enableFlag: [
          {
            required: true,
            min: 1,
            max: 1,
            message: '只能输入Y或N！',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    /** 查询单位列表 */
    fetchData() {
      //组装查询参数
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        measureCode: this.keyword.measureCode,
        measureName: this.keyword.measureName
      }

      getUmList(params).then(res => {
        if (res.code == 20000) {
          this.loading = false
          this.measureList = res.data.list
          this.pageNum = res.data.pageNum
          this.total = res.data.total
        } else {
          this.$message({
            type: 'error',
            message: '查询失败',
            duration: 1000
          })
        }
      })
    },
    handleSizeChange(val) {
      // 处理页面大小改变时触发
      this.loading = true
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // 当前页码改变时触发
      this.loading = true
      this.pageNum = val
      this.fetchData()
    },
    // 删除单个
    handleDelete(row) {

      this.$confirm('确认删除编号为["' + row.measureCode + '"]的单位?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        let data = {
          measureCode: row.measureCode
        }
        delUmone(data).then(res => {
          if (res.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功',
              duration: 1000
            })
            this.fetchData()
          } else {
            this.$message({
              type: 'error',
              message: '删除失败',
              duration: 1000
            })
          }
        })
      })
    },
    //模糊查询
    search() {
      this.loading = true
      this.fetchData()
    },
    // 重置表单
    resetSearchForm(formName) {
      this.loading = true
      this.$refs[formName].resetFields()
      this.fetchData()
    },
    //批量删除
    delSelect() {
      this.$confirm('确认删除编号为["' + this.measureCodes + '"]的单位?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.measureCodes.length != 0) {
          let params = {
            measureCodes: this.measureCodes
          }
          //也可以用拼接字符串，后端用@requestParam list<>接
          // const CodeListString = this.measureCodes.join(",");
          //       console.log("拼接的编号字符串：", CodeListString);
          delMany(params).then(res => {
            if (res.code == 20000) {
              this.$message({
                type: 'success',
                message: '删除成功',
                duration: 1000
              })
              this.loading = true
              this.fetchData()
            } else {
              this.$message({
                type: 'error',
                message: '删除失败',
                duration: 1000
              })
            }
          })
        } else {
          this.$message({
            type: 'warning',
            message: '没选中一项哦',
            duration: 1500
          })
        }
      })
    },
    //编辑更新对话框
    handleUpdate(dialogName, row) {
      this.dialogName = dialogName
      this.addDialog = true
      this.oneMeasure = row
    },
    //打开添加对话框
    openAddDialog() {
      this.dialogName = '添加单位'
      this.addDialog = true
    },
    //取消添加
    cancelAddMeasure(formName) {
      this.$refs[formName].resetFields()
      this.oneMeasure = {}
      this.loading = true
      this.fetchData()
      this.addDialog = false

    },
    //多选内容变化监听
    handleSelectionChange(val) {
      this.measureCodes = [] //每次多选前先清空数组
      this.multipleSelection = val
       // 有选中任何一项就激活批量删除按钮
      //将多选内容中book对象的bname拿出来
      for (let i = 0; i < this.multipleSelection.length; i++) {
        let mn = this.multipleSelection[i]
        this.measureCodes.push(mn.measureCode)
      }
    },
    //添加一项
    addMeasure(formName) {
      //
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.dialogName == '添加单位') {
            addUmone(this.oneMeasure).then((res) => {
              if (res.code == 20000) {
                this.$message({
                  type: 'success',
                  message: '添加成功',
                  duration: 1000
                })
                this.fetchData()
                this.addDialog = false
                this.oneMeasure = {}
              } else {
                this.$message({
                  type: 'error',
                  message: '添加失败',
                  duration: 1000
                })
              }
            })
          } else if (this.dialogName == '编辑单位') {
            updateUmone(this.oneMeasure).then((res) => {
              if (res.code == 20000) {
                this.$message({
                  type: 'success',
                  message: '更新成功',
                  duration: 1000
                })
                this.fetchData()
                this.addDialog = false
                this.oneMeasure = {}
              } else {
                this.loading = false
                this.$message({
                  type: 'error',
                  message: '更新失败',
                  duration: 1000
                })

              }
            })
          } else {
            return false
          }
        }
      })
    }

  },
  created() {
    this.fetchData()
  }
}
</script>
<style scoped>
.un {
  margin-left: 20px;
}
</style>
